package com.bw.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.bean.DataBean;
import com.bw.service.DataBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DataBeanController {

    @Autowired
    private DataBeanService dataBeanService;


    @RequestMapping("/s1")
    public JSONObject select1(){
        //创建的返回结果一定是com.alibaba.fastjson中的对象
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        //创建data对象
        JSONObject data = new JSONObject();
        result.put("data",data);


        //创建数组对象
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();
        //存入到data中
        data.put("columns",columns);
        data.put("rows",rows);

        //创建列对象
        JSONObject col1 = new JSONObject();
        col1.put("id","id");  //列中的 字段名称id
        col1.put("name","序号");

        JSONObject col2 = new JSONObject();
        col2.put("id","name");  //列中的 字段名称id
        col2.put("name","店铺的名称");

        JSONObject col3 = new JSONObject();
        col3.put("id","cta");  //列中的 字段名称id
        col3.put("name","下单金额");


        JSONObject col4 = new JSONObject();
        col4.put("id","ctb");  //列中的 字段名称id
        col4.put("name","下单次数");


        JSONObject col5 = new JSONObject();
        col5.put("id","ctc");  //列中的 字段名称id
        col5.put("name","下单人数");


        JSONObject col6 = new JSONObject();
        col6.put("id","ctd");  //列中的 字段名称id
        col6.put("name","实际金额");


        JSONObject col7 = new JSONObject();
        col7.put("id","cte");  //列中的 字段名称id
        col7.put("name","退款金额");

        //加入到列对应的数组中
        columns.add(col1);  //加入到列中
        columns.add(col2);  //加入到列中
        columns.add(col3);  //加入到列中
        columns.add(col4);  //加入到列中
        columns.add(col5);
        columns.add(col6);
        columns.add(col7);

        //查询业务数据
        List<DataBean> beans = dataBeanService.select1();
        //遍历
        for (DataBean bean : beans) {
            //对应的行数据
            JSONObject row = new JSONObject();
            row.put("id",bean.getId());
            row.put("name",bean.getName());
            row.put("cta",bean.getCta());
            row.put("ctb",bean.getCtb());
            row.put("ctc",bean.getCtc());
            row.put("ctd",bean.getCtd());
            row.put("cte",bean.getCte());

            //加入到行对应的数组中
            rows.add(row);
        }

        return result;
    }


    @RequestMapping("/s2")
    public JSONObject select2(){
        //创建的返回结果一定是com.alibaba.fastjson中的对象
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        //创建数组对象
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();

        data.put("columns",columns); //存入到data对象中
        data.put("rows",rows); //存入到data对象中

        JSONObject col1 = new JSONObject();
        col1.put("id","name");
        col1.put("name","天数");


        JSONObject col2 = new JSONObject();
        col2.put("id","cta");
        col2.put("name","直营金额");

        JSONObject col3 = new JSONObject();
        col3.put("id","ctb");
        col3.put("name","加盟金额");

        columns.add(col1); //第一列
        columns.add(col2); //第二列
        columns.add(col3); //第三列
        //查询业务数九
        List<DataBean> beans = dataBeanService.select2();
        //遍历业务数据
        for (DataBean bean : beans) {
            JSONObject row = new JSONObject();
            row.put("name",bean.getName());
            row.put("cta",bean.getCta());
            row.put("ctb",bean.getCtb());
            //加入rows中
            rows.add(row);
        }

        return result;
    }



    @RequestMapping("/s3")
    public JSONObject select3(){
        //创建的返回结果一定是com.alibaba.fastjson中的对象
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");
        
        JSONObject data = new JSONObject();
        result.put("data",data);

        //创建出来的对象或者数组  要立刻给放入到相应的对象中
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();

        data.put("categories",categories);
        data.put("series",series);

        JSONObject obj = new JSONObject();
        series.add(obj);
        obj.put("name","菜品评价次数");

        JSONArray obj_data = new JSONArray();
        obj.put("data",obj_data);




        JSONObject obj1 = new JSONObject();
        series.add(obj1);
        obj1.put("name","菜品好评次数");

        JSONArray obj_data1 = new JSONArray();
        obj1.put("data",obj_data1);


        JSONObject obj2 = new JSONObject();
        series.add(obj2);
        obj2.put("name","菜品评价均分");

        JSONArray obj_data2 = new JSONArray();
        obj2.put("data",obj_data2);

        //执行业务数据查询
        List<DataBean> beans = dataBeanService.select3();
        for (DataBean bean : beans) {
            categories.add(bean.getName()); //加入的名称
            obj_data.add(bean.getCta()); //菜品评价次数
            obj_data1.add(bean.getCtb()); // 菜品的好评次数
            obj_data2.add(bean.getCtc()); //菜品评价均分
        }

        return result;
    }


    @RequestMapping("/s4")
    public JSONObject select4(){
        //创建的返回结果一定是com.alibaba.fastjson中的对象
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();

        data.put("categories",categories);
        data.put("series",series);

        //下单金额
        JSONObject obj1 = new JSONObject();
        series.add(obj1);
        obj1.put("name","下单金额");

        JSONArray obj1_data = new JSONArray();
        obj1.put("data",obj1_data);




        //下单次数
        JSONObject obj2 = new JSONObject();
        series.add(obj2);
        obj2.put("name","下单次数");

        JSONArray obj2_data = new JSONArray();
        obj2.put("data",obj2_data);


        //下单人数
        JSONObject obj3 = new JSONObject();
        series.add(obj3);
        obj3.put("name","下单人数");

        JSONArray obj3_data = new JSONArray();
        obj3.put("data",obj3_data);




        //从业务数据查询
        List<DataBean> beans = dataBeanService.select4();
        //遍历数据
        for (DataBean bean : beans) {
            categories.add(bean.getName()); //时段的名称
            obj1_data.add(bean.getCta()); // 下单金额
            obj2_data.add(bean.getCtb()); // 下单次数
            obj3_data.add(bean.getCtc()); // 下单人数
        }

        return result;
    }

}
